package main.ressik;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class CalendarController {
	
	private FoodDb fdb;
	
	
	
	
	/**
	 * Constructor
	 */
	public CalendarController(){
		fdb = FoodDb.getInstance();
	}
	
	
	
	
	
	/**
	 *  Returns a List of foods that have been added for that particular day.
	 */
	public ArrayList<String> getFoodsForDay(String date){
		return fdb.getFoodsForDay(date);
	}
	
	
	
	
	
	/**
	 * The event that occurs when the "See Foods" button is clicked from the Calendar tab.
	 * This brings up a list of all the foods that user has added to that particular day.
	 */
	@SuppressWarnings("unchecked")
	public void seeFoodsClick(final Main main, final FoodListView flv){
		final String fullDate = getFullDate();
		final ListView list = new ListView(main);
		final ListAdapter lAdapter = new ArrayAdapter(main,android.R.layout.simple_list_item_1,getFoodsForDay(fullDate));
    	list.setAdapter(lAdapter);
    	list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
			public void onItemClick(AdapterView av,View v, int position, long id){
				flv.getFoodListController().listViewClick(id, main,list);
			}
		});
    	list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
			public boolean onItemLongClick(AdapterView av, View v, int position, long id){
				int newId = (int)id;
				final String food = (String) av.getItemAtPosition(newId);
				final ListView menuList = new ListView(main);
				ArrayList<String> menuItems = new ArrayList<String>();
				menuItems.add("Delete From Day");
				final ListAdapter lAdapter2 = new ArrayAdapter(main,android.R.layout.simple_list_item_1,menuItems);
				menuList.setAdapter(lAdapter2);
				final AlertDialog menuDialog = new AlertDialog.Builder(main).setTitle(food).setView(menuList).show();
				menuList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
					public void onItemClick(AdapterView av,View v, int position, long id){
						int newId = (int)id;
						String itemClicked = (String) av.getItemAtPosition(newId);
						if (itemClicked.equals("Delete From Day")){
							onDeleteFromDayClick(food, fullDate, main);
							list.setAdapter(new ArrayAdapter(main,android.R.layout.simple_list_item_1,getFoodsForDay(fullDate)));
							menuDialog.hide();
						}
					}
				});
				return true;
			}
		});
    	new AlertDialog.Builder(main).setTitle("Foods").setView(list).show();
	}
	
	
	
	
	
	/**
	 * The event that occurs when the "See Details" button is clicked from the Calendar tab.
	 * It pulls up an alert dialog box with all the details for the day (ie. total foods eaten,
	 * total points, cals, fat, fiber, etc).
	 */
	public void seeDetailsClick(){
		String fullDate = getFullDate();
		String foodCount = new Integer(fdb.getFoodsForDay(fullDate).size()).toString();
		Cursor foods = fdb.getFoodCursorForDay(fullDate);
		int calTotal = 0;
		int fatTotal = 0;
		int fiberTotal = 0;
		int pointTotal = 0;
		if (foods != null){
	    	while(foods.moveToNext()){
	    		String food = foods.getString(foods.getColumnIndex("name"));
	    		Cursor singleFood = fdb.getSingleFood(food);
	    		while (singleFood.moveToNext()){
		    		int cals = new Integer(singleFood.getString(singleFood.getColumnIndex("cals"))); 
		    		int fat = new Integer(singleFood.getString(singleFood.getColumnIndex("fat"))); 
		    		int fiber = new Integer(singleFood.getString(singleFood.getColumnIndex("fiber"))); 
		    	    calTotal += cals;
		    	    fatTotal += fat;
		    	    fiberTotal += fiber;
		    	    if (fiber > 4) fiber = 4;
		    	    int pointsInt = (cals/50) + (fat/12) - (fiber/5);
		    	    pointTotal += pointsInt;
	    		}
	    	}
		}
		new AlertDialog.Builder(Main.getInstance()).setTitle("Day Info:").setMessage("Total Foods Eaten: " + foodCount + "\n"
				+ "Total Calories: " + calTotal + " \n" 
				+ "Total Fat: " + fatTotal + "\n"
				+ "Total Fiber: " + fiberTotal + "\n"
				+ "Total Points: " + pointTotal).show();
	}
	
	
	
	
	
	/**
	 * When the user taps and holds a food in the list that comes up after the 
	 * "See Foods" button is clicked, they are given the option to delete the food
	 * from that calendar day.  This is the event that is called when that occurs.
	 */
	public void onDeleteFromDayClick(String food, String date, Main main){
		fdb.deleteFoodFromDay(food,date);
		Toast.makeText(main, food + " Deleted from Day", Toast.LENGTH_SHORT).show();
	}
	
	
	
	
	
	/**
	 * This parses out the details from the calendar widget and stores
	 * the data as a String called fullDate.  The format is in DDMMYYYY.
	 */
	public String getFullDate(){
		DatePicker dp  = (DatePicker) Main.getInstance().findViewById(R.id.date_picker);
		String day = new Integer(dp.getDayOfMonth()).toString();
		String month = new Integer(dp.getMonth()).toString();
		String year = new Integer(dp.getYear()).toString();
		String fullDate = day + month + year;
		return fullDate;
	}
	
	
	
}
